package com.paipaideli.ui.mine.realname;

import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.BuildConfig;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterActivity;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.TStatusView;
import com.paipaideli.common.utils.StringUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.ui.MainActivity;
import com.paipaideli.ui.mine.safety.event.LogoutEvent;
import com.paipaideli.ui.webView.WebAcyivity;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by 实名认证
 */
public class RealNameActivity extends BasePresenterActivity {

    Unbinder unbinder;
    @BindView(R.id.top_back)
    RelativeLayout top_back;
    @BindView(R.id.top_title)
    TextView top_title;
    @BindView(R.id.image_realname_check)
    ImageView image_realname_check;
    @BindView(R.id.et_realname_name)
    EditText etRealnameName;
    @BindView(R.id.et_realname_idcard)
    EditText etRealnameIdcard;
    @BindView(R.id.btn_realname_rz)
    Button btnRealnameRz;
    @BindView(R.id.tv_register_ys)
    TextView tv_register_ys;
    @BindView(R.id.bg_statusview)
    TStatusView bg_statusview;

    private boolean flag = true;

    @Override
    public int getContentView() {
        return R.layout.activity_realname;
    }

    @Override
    public void initView() {
        unbinder = ButterKnife.bind(this);
        top_title.setText("实名认证");
        //返回
        top_back.setOnClickListener(o ->
                finish()
        );

        //实名认证
        addDisposable(RxView.clicks(btnRealnameRz)
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(o -> {
                    realName();
                }));

        //隐私权限的选中
        addDisposable(RxView.clicks(image_realname_check)
                .subscribe(o -> {
                    if (flag) {
                        flag = false;
                        image_realname_check.setImageResource(R.mipmap.login_check);
                    } else {
                        flag = true;
                        image_realname_check.setImageResource(R.mipmap.login_nocheck);
                    }
                }));

        tv_register_ys.setOnClickListener(v->{
            WebAcyivity.open(getCurrentActivity(), BuildConfig.h5 + "agreement.html","隐私协议");
        });
    }

    private void realName() {
        String name = etRealnameName.getText().toString().trim();
        String idcard = etRealnameIdcard.getText().toString().trim();
        if (!StringUtil.isEmpty(name)) {
            if (!StringUtil.isEmpty(idcard)) {
                if (flag) {
                    ToastUtil.show("请同意隐私协议！");
                } else {
                    JSONObject requestData = new JSONObject();
                    try {
                        requestData.put("realname", name);
                        requestData.put("idcard", idcard);
                    } catch (Exception e) {
                        e.getMessage();
                    }
                    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
                    bg_statusview.showLoading();
                    Api.getInstanceGson().certificat(body)
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(request -> {
                                ToastUtil.show(request.msg);
                                if (request.code.equals("200")) {
                                    MainActivity.openSingleTop(getCurrentActivity());
                                    LogoutEvent logoutEvent=new LogoutEvent();
                                    logoutEvent.setCheck(3);
                                    EventBus.getDefault().post(logoutEvent);
                                    finish();
                                }
                                bg_statusview.finish();

                            }, throwable -> {
                                bg_statusview.finish();
                                Log.e(TAG, "error", throwable);
                            });
                }
            } else {
                ToastUtil.show("请输入身份证号码！");
            }
        } else {
            ToastUtil.show("请输入姓名！");
        }
    }
}
